﻿/*
Copyright (C) 2007 Nathanael Ness

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var nowp = new NowPlaying(onSongChange);
var top = new top44();
var img = new images(ImageLoaded);
var amazon_art = new AmazonArt();
var fade_time = 5000;
var anim_cnt = 1;
var anim;
var BORDER_OFFSET = 16;
var TEXT_OFFSET = 32;
var expanded = false;
var image_url = "";
var docked_width;
var docked_height;

function view_onOpen() {
	var date = formatDate(new Date(new Date().toUTCString()),'E, d MM yyyy kk:mm:ss').toString() + " GMT";
  options.putDefaultValue("Date_updated", date);
	options.putDefaultValue("banner", "ON");
	options.putDefaultValue("size", "0");
  top.update();
  nowp.request_update();
  anim = beginAnimation("fade_title()", 255, 0, fade_time);
  timer = setInterval( "AnimateLoading()", fade_time);
  plugin.onAddCustomMenuItems = AddCustomMenuItems;
	if (options.getValue("banner") == "OFF") {
		TEXT_OFFSET = 0;
		Effect.visible = false;
		if(expanded) {
			_onPopout();
		} else {
			_onPopin();
		}
	}
}

function onSongChange() {
	album.innerText = nowp.getAlbum();
	artist.innerText = nowp.getArtist();
  title.innerText = nowp.getTitle();
	var tops = top.search(nowp.getTitle(),nowp.getArtist());
	if(tops[0] != "") {
		top_label.innerText = "Effect Top 44 Song #" + tops[2];
	} else {
		if (tops[1] != "") {
			top_label.innerText = "Effect Top 44 Artist";
		} else {
			top_label.innerText = "";
		}
	}
	amazon_art.fetch_art(nowp.getArtist(), nowp.getAlbum());
	image_url = amazon_art.getURL();
	if (image_url != "") {
		link.innerText = "Buy this album on Amazon.com";
	} else {
		link.innerText = "";
	}
	link.href = "http://amazon.com/o/asin/" + amazon_art.getASIN();
	if (image_url == "") {
		image_url = top.getURL();
	}
	if (image_url != "") {
		img.request(image_url);
	} else {
		album_image.src = "stock_images\\no_album.png";
	}
}

function ImageLoaded() {
	album_image.src = img.getImage();
}

function _onOptionChange() {
	if (options.getValue("banner") == "OFF") {
		TEXT_OFFSET = 0;
		Effect.visible = false;
	}
	if (options.getValue("banner") == "ON") {
		TEXT_OFFSET = 32;
		Effect.visible = true;
	}
	if(expanded) {
		_onPopout();
	} else {
		_onPopin();
	}
}

function _onPopin() {
	expanded = false;
	//view.resizeTo(docked_width, docked_height);
	view.width = docked_width;
	if (docked_height > 128) {
		view.height = docked_height;
	}
	anim_cnt = 0;
	AnimateLoading();
	clearInterval(timer);
	timer = setInterval( "AnimateLoading()", fade_time);
	album.x = 0;
	album.width = view.width - BORDER_OFFSET * 2;
	album.height = view.height - BORDER_OFFSET * 2;
	album.y = TEXT_OFFSET;
	artist.x = 0;
	artist.width = view.width - BORDER_OFFSET * 2;
	artist.height = view.height - BORDER_OFFSET * 2;
	artist.y = TEXT_OFFSET;
	title.x = 0;
	title.width = view.width - BORDER_OFFSET * 2;
	title.height = view.height - BORDER_OFFSET * 2;
	title.y = TEXT_OFFSET;
	album_label.y = mid.height - BORDER_OFFSET;
	artist_label.y = mid.height - BORDER_OFFSET;
	title_label.y = mid.height - BORDER_OFFSET;
	artist.visible = false;
	artist_label.visible = false;
	album.visible = false;
	album_label.visible = false;
	link.visible = false;
	album_image.visible = false;
	top_label.visible = false;
}

function _onPopout() {
	expanded = true;
	docked_width = view.width;
	if (view.height > 128) {
		docked_height = view.height;
	}
	//view.resizeTo(250,400);
	view.width = 250;
	view.height = 400;
	cancelAnimation(anim);
	clearInterval(timer);
	mid.width = view.width - (BORDER_OFFSET * 2);
	mid.height = view.height - (BORDER_OFFSET * 2);
	title.height = 64;
	artist.height = 64;
	album.height = 64;
	title.x = 0;
	title.y = TEXT_OFFSET;
	artist.x = 0;
	artist.y = TEXT_OFFSET + title.height;
	album.x = 0;
	album.y = TEXT_OFFSET + title.height + artist.height;
	title.width = mid.width;
	artist.width = mid.width;
	album.width = mid.width;
	title_label.y = TEXT_OFFSET + (album.height / 2);
	artist_label.y = TEXT_OFFSET + title.height + (artist.height / 2);
	album_label.y = TEXT_OFFSET + title.height + artist.height + (title.height / 2);
	title.visible = true;
	title_label.visible = true;
	artist.visible = true;
	artist_label.visible = true;
	album.visible = true;
	album_label.visible = true;
	title.opacity = 255;
	artist.opacity = 255;
	album.opacity = 255;
	link.visible = true;
	link.y = TEXT_OFFSET + title.height + artist.height + album.height + top_label.height;
	album_image.visible = true;
	album_image.y = TEXT_OFFSET + title.height + artist.height + album.height + top_label.height + link.height;
	top_label.visible = true;
	top_label.y = TEXT_OFFSET + title.height + artist.height + album.height;
	top.search(title.innerText, artist.innerText);
}

function resize() {
	topp.width = view.width - BORDER_OFFSET * 2;
	bottom.width = view.width - BORDER_OFFSET * 2;
	left.height = view.height - BORDER_OFFSET * 2;
	right.height = view.height - BORDER_OFFSET * 2;
	bottom.y = view.height - BORDER_OFFSET;
	right.x = view.width - BORDER_OFFSET;
	top_right.x = view.width - BORDER_OFFSET;
	bottom_right.x = view.width - BORDER_OFFSET;
	bottom_right.y = view.height - BORDER_OFFSET;
	bottom_left.y = view.height - BORDER_OFFSET;
	mid.width = view.width - (BORDER_OFFSET * 2);
	mid.height = view.height - (BORDER_OFFSET * 2);
	album_label.x = view.width - 85;
	artist_label.x = view.width - 85;
	title_label.x = view.width - 85;
	title.width = mid.width;
	artist.width = mid.width;
	album.width = mid.width;
	if (!expanded) {
		album_label.y = mid.height - BORDER_OFFSET;
		artist_label.y = mid.height - BORDER_OFFSET;
		title_label.y = mid.height - BORDER_OFFSET;
		album.height = view.height - (BORDER_OFFSET * 2);
		artist.height = view.height - (BORDER_OFFSET * 2);
		title.height = view.height - (BORDER_OFFSET * 2);
	}
}

function single_click() {
	if(!expanded) {
		cancelAnimation(anim);
		AnimateLoading();
	}
}

function AnimateLoading() {
  if (anim) {
    cancelAnimation(anim);
    anim = null;
  }
	if (anim_cnt == 0) {
		album.visible = false;
		title.visible = true;
		album_label.visible = false;
		title_label.visible = true;
		anim = beginAnimation("fade_title()", 255, 0, fade_time);
		anim_cnt++;
		return;
	}
	if (anim_cnt == 1) {
		title.visible = false;
		artist.visible = true;
		title_label.visible = false;
		artist_label.visible = true;
		anim = beginAnimation("fade_artist()", 255, 0, fade_time);
		anim_cnt++;
		return;
	}
	if (anim_cnt == 2) {
		artist.visible = false;
		album.visible = true;
		artist_label.visible = false;
		album_label.visible = true;
		anim = beginAnimation("fade_album()", 255, 0, fade_time);
		anim_cnt = 0;
		return;
	}
}

function fade_title() {
		title.opacity = event.value;
}

function fade_artist() {
		artist.opacity = event.value;
}

function fade_album() {
		album.opacity = event.value;
}

function AddCustomMenuItems(menu) {
  menu.AddItem("Reset", 0, reset);
} 

function reset()
{
	nowp.reset();
}